package cn.memorynine.consumer.service;

import cn.memorynine.dao.entity.Activity;
import cn.memorynine.dao.entity.Consumer;
import cn.memorynine.dao.entity.ForumPost;
import cn.memorynine.dao.entity.ShopCommodity;
import cn.memorynine.dao.mapper.ActivityMapper;
import cn.memorynine.dao.mapper.ConsumerMapper;
import cn.memorynine.dao.mapper.ForumPostMapper;
import cn.memorynine.dao.mapper.ShopCommodityMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.memorynine.common.model.ResponseWrapper;
import cn.memorynine.consumer.model.index.*;

import java.util.ArrayList;
import java.util.List;

/**
* 首页服务
* @author 代码生成器
*/
@Slf4j
@Service
public class IndexService {

    @Autowired
    private ShopCommodityMapper shopCommodityMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ForumPostMapper forumPostMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    /**
     * 1.获取热门商品
     * @return
     */
    public ResponseWrapper HotCommondityList(HotCommondityListParam param){
        List<HotCommondityListResult> hotCommondityListResults = new ArrayList<>();
        List<ShopCommodity> shopCommodities = shopCommodityMapper.selectList(
                Wrappers.<ShopCommodity>lambdaQuery()
                        .eq(ShopCommodity::getStateId,10)
                        .last("LIMIT " + param.getQuantity())
        );
        for (ShopCommodity shopCommodity : shopCommodities) {
            HotCommondityListResult hotCommondityListResult = new HotCommondityListResult();
            hotCommondityListResult.setCommondityAid(shopCommodity.getAid());
            hotCommondityListResult.setCommondityName(shopCommodity.getName());
            hotCommondityListResult.setCommondityImg(shopCommodity.getImage());
            hotCommondityListResult.setPrice(shopCommodity.getPrice());
            hotCommondityListResults.add(hotCommondityListResult);
        }

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(hotCommondityListResults);
    }

    /**
     * 2.获取热门帖子
     * @return
     */
    public ResponseWrapper HotPostList(HotPostListParam param){
        List<HotPostListResult> hotPostListResults = new ArrayList<>();
        List<ForumPost> forumPosts = forumPostMapper.selectList(
                Wrappers.<ForumPost>lambdaQuery()
                        .eq(ForumPost::getIsDelete,0)
                        .last("LIMIT " + param.getQuantity())
        );
        for (ForumPost forumPost : forumPosts) {
            HotPostListResult hotPostListResult = new HotPostListResult();
            hotPostListResult.setPostAid(forumPost.getAid());
            hotPostListResult.setPostImg(forumPost.getImage());
            hotPostListResult.setPostTitle(forumPost.getTitle());
            hotPostListResult.setPostBrief(forumPost.getBriefContent());
            hotPostListResult.setPostLastTime(forumPost.getTimeLastUpdate());
            hotPostListResults.add(hotPostListResult);
        }
        //最后一步，返回结果
        return ResponseWrapper.markSuccess(hotPostListResults);
    }

    /**
     * 3.获取热门活动
     * @return
     */
    public ResponseWrapper HotActivity(HotActivityParam param){
        List<HotActivityResult> hotActivityResults = new ArrayList<>();
        List<Activity> activities = activityMapper.selectList(
                Wrappers.<Activity>lambdaQuery()
                        .eq(Activity::getStateId,10)
                        .last("LIMIT " + param.getQuantity())
        );
        for (Activity activity : activities) {
            HotActivityResult hotActivityResult = new HotActivityResult();
            hotActivityResult.setActivityAid(activity.getAid());
            hotActivityResult.setActivityImg(activity.getImage());
            hotActivityResult.setActivityTitle(activity.getTitle());
            hotActivityResult.setActivityBriefContent(activity.getBriefContent());
            String create_consumer_name = consumerMapper.selectList(
                    Wrappers.<Consumer>lambdaQuery()
                            .eq(Consumer::getAid,activity.getActivityConsumerAid()))
                    .get(0).getName();
            hotActivityResult.setActivityConsumerName(create_consumer_name);
            hotActivityResults.add(hotActivityResult);
        }
        //最后一步，返回结果
        return ResponseWrapper.markSuccess(hotActivityResults);
    }

}